# -*- encoding=UTF-8 -*-

from ganstore import db
from datetime import datetime


class Tread(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(32))
    price = db.Column(db.Integer)
    tread_wide = db.Column(db.Integer)
    flat_radio = db.Column(db.Integer)
    diameter = db.Column(db.Integer)
    seller_id = db.Column(db.Integer, db.ForeignKey('seller.id'))
    images = db.relationship('Image', backref='tread', lazy='dynamic')

    def __init__(self, name, price, tread_wide, flat_radio, diameter, seller_id):
        self.name = name
        self.price = price
        self.tread_wide = tread_wide
        self.flat_radio = flat_radio
        self.diameter = diameter
        self.seller_id = seller_id

    def __repr__(self):
        return '<Tread %d %s>' % (self.id, self.content)


class Seller(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(64))
    address = db.Column(db.String(512))
    contact = db.Column(db.String(64))
    phone = db.Column(db.String(32))
    treads = db.relationship('Tread', backref='seller')

    def __init__(self, name, address, contact, phone):
        self.name = name
        self.address = address
        self.contact = contact
        self.phone = phone

    def __repr__(self):
        return '<Seller %d %s>' % (self.id, self.name)


class Image(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    url = db.Column(db.String(512))
    tread_id = db.Column(db.Integer, db.ForeignKey('tread.id'))

    def __init__(self, url, tread_id):
        self.url = url
        self.tread_id = tread_id

    def __repr__(self):
        return '<Image %d %s>' % (self.id, self.url)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(32))
    salt = db.Column(db.String(32))
    # 0表示非会员  1表示青铜会员 2表示白银会员 3表示黄金会员
    vip_level = db.Column(db.String(32), default='0')
    vip_deadline = db.Column(db.DateTime)
    phone = db.Column(db.String(32))
    # 积分记录
    rank_record = db.relationship('Rankrecord', backref='user', lazy='dynamic')
    # 保单记录
    policy_record = db.relationship('Policyrecord', backref='user', lazy='dynamic')

    def __init__(self, username, password, phone='', salt=''):
        self.username = username
        self.password = password
        self.phone = phone
        self.salt = salt

    def __repr__(self):
        return '<User %d %s>' % (self.id, self.username)


# 积分记录
class Rankrecord(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    created_date = db.Column(db.DateTime)
    record_add_reduce = db.Column(db.String(64))
    record_event = db.Column(db.String(32))
    record_change = db.Column(db.String(32))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __init__(self, record_add_reduce, record_event, record_change, user_id):
        self.record_add_reduce = record_add_reduce
        self.record_event = record_event
        self.record_change = record_change
        self.user_id = user_id
        self.created_date = datetime.now()

    def __repr__(self):
        return '<User %d %s>' % (self.id, self.record_change)

# 保单记录
class Policyrecord(db.Model):
    __name__ = 'policyrecord'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    car_num = db.Column(db.String(32))
    created_datetime = db.Column(db.DateTime)
    policy_event = db.Column(db.String(32))
    policy_image = db.relationship('PolicyImage', backref='policyrecord', lazy='dynamic')

    def __init__(self, car_num, policy_event, user_id, created_datetime):
        self.car_num = car_num
        self.policy_event = policy_event
        self.user_id = user_id
        self.created_datetime = created_datetime


    def __repr__(self):
        return '<User %d %s>' % (self.id, self.car_num)


# 保单记录里面的图片
class PolicyImage(db.Model):
    __name__ = 'policyimage'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    policy_id = db.Column(db.Integer, db.ForeignKey('policyrecord.id'))
    url = db.Column(db.String(512))

    def __init__(self, url, policy_id):
        self.url = url
        self.policy_id = policy_id

    def __repr__(self):
        return '<Image %d %s>' % (self.id, self.url)


